package top.ochiamalu.aioj.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ochiamalu.aioj.common.BaseResponse;
import top.ochiamalu.aioj.common.ErrorCode;
import top.ochiamalu.aioj.domain.dto.question.AddQuestionDTO;
import top.ochiamalu.aioj.domain.dto.question.DeleteQuestionDTO;
import top.ochiamalu.aioj.domain.dto.question.GenerateDTO;
import top.ochiamalu.aioj.domain.dto.question.PageQuestionDTO;
import top.ochiamalu.aioj.domain.dto.question.UpdateQuestionDTO;
import top.ochiamalu.aioj.domain.vo.question.GenerateVO;
import top.ochiamalu.aioj.domain.vo.question.HotQuestionVO;
import top.ochiamalu.aioj.domain.vo.question.QuestionListVO;
import top.ochiamalu.aioj.domain.vo.question.QuestionVO;
import top.ochiamalu.aioj.domain.vo.question.RecentAcceptVO;
import top.ochiamalu.aioj.domain.vo.question.RecentUpdateQuestionVO;
import top.ochiamalu.aioj.domain.vo.question.SolveHistoryVO;
import top.ochiamalu.aioj.domain.vo.question.UpdateQuestionVO;
import top.ochiamalu.aioj.exception.BusinessException;
import top.ochiamalu.aioj.service.QuestionService;
import top.ochiamalu.aioj.util.ResultUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问题控制器
 *
 * @author OchiaMalu
 * @date 2024/12/09
 */
@RestController
@RequestMapping("/questions")
@Slf4j
public class QuestionController {

    @Resource
    private QuestionService questionService;

    /**
     * 获取题目列表
     *
     * @return {@link BaseResponse }<{@link List }<{@link QuestionListVO }>>
     */
    @GetMapping("/list")
    public BaseResponse<List<QuestionListVO>> listQuestions() {
        log.info("Start obtaining the list of questions");
        List<QuestionListVO> questionListVOList = questionService.listQuestions();
        log.info("Successfully obtained question list, quantity: {}", questionListVOList.size());
        return ResultUtils.success(questionListVOList);
    }

    /**
     * 题目分页
     *
     * @param pageQuestionDTO 页面问题dto
     * @return {@link BaseResponse }<{@link Page }<{@link QuestionListVO }>>
     */
    @PostMapping("/page")
    public BaseResponse<Page<QuestionListVO>> pageQuestions(@RequestBody PageQuestionDTO pageQuestionDTO) {
        log.info("Start obtaining question list");
        Page<QuestionListVO> page = questionService.pageQuestions(pageQuestionDTO);
        return ResultUtils.success(page);
    }

    /**
     * 添加问题
     *
     * @param addQuestionDTO 添加问题dto
     * @return {@link BaseResponse }<{@link Long }>
     */
    @PostMapping("/add")
    @SaCheckPermission("question:add")
    public BaseResponse<Long> addQuestion(@RequestBody AddQuestionDTO addQuestionDTO) {
        log.info("Start adding a new question");
        if (StringUtils.isAnyBlank(addQuestionDTO.getTitle(), addQuestionDTO.getContent())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入正确的信息");
        }
        if (addQuestionDTO.getDifficulty() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入困难度");
        }
        if (addQuestionDTO.getTestCaseList().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入测试用例");
        }
        Long questionId = questionService.addQuestion(addQuestionDTO);
        log.info("Successfully added a new question, questionId: {}", questionId);
        return ResultUtils.success(questionId);
    }

    /**
     * 通过id获取问题
     *
     * @param id 身份证件
     * @return {@link BaseResponse }<{@link QuestionVO }>
     */
    @GetMapping("/{id}")
    public BaseResponse<QuestionVO> getQuestionById(@PathVariable Long id) {
        QuestionVO questionVO = questionService.getQuestionById(id);
        log.info("Successfully obtained question information, questionId: {}", id);
        return ResultUtils.success(questionVO);
    }

    /**
     * 通过id获取问题详细信息
     *
     * @param id 身份证件
     * @return {@link BaseResponse }<{@link UpdateQuestionVO }>
     */
    @GetMapping("/detail/{id}")
    public BaseResponse<UpdateQuestionVO> getQuestionDetailById(@PathVariable Long id) {
        UpdateQuestionVO questionVO = questionService.getQuestionDetailById(id);
        log.info("Successfully obtained question information, questionId: {}", id);
        return ResultUtils.success(questionVO);
    }

    /**
     * 更新问题
     *
     * @param updateQuestionDTO 更新问题dto
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateQuestion(@RequestBody UpdateQuestionDTO updateQuestionDTO) {
        log.info("Start updating question information");
        boolean result = questionService.updateQuestion(updateQuestionDTO);
        log.info("Successfully updated question information, questionId: {}", updateQuestionDTO.getId());
        return ResultUtils.success(result);
    }

    /**
     * 列出热门问题
     *
     * @return {@link BaseResponse }<{@link List }<{@link HotQuestionVO }>>
     */
    @GetMapping("/hot")
    public BaseResponse<List<HotQuestionVO>> listHotQuestions() {
        List<HotQuestionVO> hotQuestionVOList = questionService.listHotQuestions();
        return ResultUtils.success(hotQuestionVOList);
    }

    /**
     * 列出最近更新问题
     *
     * @return {@link BaseResponse }<{@link List }<{@link RecentUpdateQuestionVO }>>
     */
    @GetMapping("/recentUpdate")
    public BaseResponse<List<RecentUpdateQuestionVO>> listRecentUpdateQuestions() {
        List<RecentUpdateQuestionVO> recentUpdateQuestionVOList = questionService.listRecentUpdateQuestions();
        return ResultUtils.success(recentUpdateQuestionVOList);
    }

    /**
     * 删除题目
     *
     * @param deleteQuestionDTO 删除题目DTO
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteQuestionDTO deleteQuestionDTO) {
        if (deleteQuestionDTO.getId() == null || deleteQuestionDTO.getId() <= 0) {
            log.error("Delete question failed due to invalid id");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入正确的id");
        }
        boolean result = questionService.deleteQuestion(deleteQuestionDTO.getId());
        return ResultUtils.success(result);
    }

    /**
     * 列出最近接受问题
     *
     * @param account 账户
     * @return {@link BaseResponse }<{@link List }<{@link RecentAcceptVO }>>
     */
    @GetMapping("/recent/{account}")
    public BaseResponse<List<RecentAcceptVO>> listRecentAcceptQuestions(@PathVariable String account) {
        List<RecentAcceptVO> recentAcceptVOList = questionService.listRecentAcceptQuestions(account);
        return ResultUtils.success(recentAcceptVOList);
    }

    /**
     * 解决历史
     *
     * @param account 账户
     * @return {@link BaseResponse }<{@link SolveHistoryVO }>
     */
    @GetMapping("/history/solve/{account}")
    public BaseResponse<SolveHistoryVO> getSolveHistory(@PathVariable("account") String account) {
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SolveHistoryVO solveHistoryVO = questionService.getSolveHistory(account);
        return ResultUtils.success(solveHistoryVO);
    }

    /**
     * 生成
     *
     * @param generateDTO 生成dto
     * @return {@link BaseResponse }<{@link GenerateVO }>
     */
    @PostMapping("/generate")
    public BaseResponse<GenerateVO> generate(@RequestBody GenerateDTO generateDTO) {
        GenerateVO generateVO = questionService.generate(generateDTO);
        return ResultUtils.success(generateVO);
    }
}
